﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AnLe.Models;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Collections;
using AnLe.Helper;

namespace AnLe.Controllers
{
    public class AdministratorController : Controller
    {
        //
        // GET: /Administrator/

        public ActionResult Index()
        {
            if (Session["username"] == null || Session["username"] != "")
            {
                return View();
            }
            else { return RedirectToAction("Login", "Account"); }
            return View();
        }

        //User control LeftMenu
        public ActionResult LeftMenuAdmin()
        {
            if (Session["userID"] == null || Session["username"] == "")
            {
                return RedirectToAction("Login", "Account");
            }            
            string userID = Session["userID"].ToString();
            string strHTML = "<div id='welcome'><span>Welcome </span><strong>" + Session["username"] + "</strong></div>";
            Connection con = new Connection();
            SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.UserRoles INNER JOIN roles ON UserRoles.RoleID = roles.RoleID WHERE UserID ='" + userID + "'");
            DataTable dtSource = con.GetDataTable(cmd);
            DataRow[] drParent = dtSource.Select("ParentID = 0");
            for (int i = 0; i < drParent.Length; i++)
            {
                DataRow[] drChild = dtSource.Select("ParentID =" + drParent[i]["RoleID"].ToString());
                if (drChild.Length > 0)
                {
                    strHTML += "<div class='homeSideNav' id='groupsNav" + i.ToString() + "'><div class='navHeader'><a onclick='hideNavMenu('prm" + i.ToString() + "');' href='#'><div class='clearfix'><div class='lfloat'>" + drParent[i]["RoleName"].ToString() + "</div></div></a></div><ul class='navContent' id='prm" + i.ToString() + "' style='display: block;'>";
                    for (int j = 0; j < drChild.Length; j++)
                    {
                        strHTML += "  <li class='navItems'><a href='" + Url.Action(drChild[j]["RoleName"].ToString(), drChild[j]["Controller"].ToString()) + "' class='item clearfix sortableItem'><div><img class='imageAdminMenu' src='" + Url.Content(drChild[j]["ImgMenu"].ToString()) + "' alt=''><div class='linkWrap noCount'>" + drChild[j]["Description"].ToString() + "</div></div></a></li>";
                    }
                    strHTML += " </ul></div>";
                }
            }
            ViewData["strHTML"] = strHTML;
            return View();
        }

        //Load images
        public ActionResult UploadImage()
        {
            string strHtml = "<div id='imgMain'>";
            string strLine = "<div class='img-column'><a href='#' class='img-a'><img  onclick=\"submitPage('[strVAL]')\" src='[strSRC]' height='120' width='150' class='img-img' /></a><a onclick=\"submitPage('[strVAL]')\" href='#'>[strFileName]</a></div>";
            int numCol = 6;
            int totalRow = 0;
            int numOut = 0;
            DirectoryInfo diFiles = new DirectoryInfo(Server.MapPath("~/Uploads/Images/"));

            String[] exts = { "*.png", "*.bmp", "*.gif", "*.jpg", "*.jpeg", "*.tif" };

            ArrayList files = new ArrayList();

            foreach (string ext in exts)
            {
                files.AddRange(diFiles.GetFiles(ext, SearchOption.AllDirectories));
            }

            FileInfo[] fi = (FileInfo[])files.ToArray(typeof(FileInfo));
            StringHelper.countRow(fi.Length, numCol, ref totalRow, ref numOut);

            if (numOut == 0)
            {

                for (int i = 1; i <= totalRow; i++)
                {
                    strHtml += "<div class='img-line'>";
                    for (int j = ((i - 1) * numCol); j < (i * numCol); j++)
                    {
                        string strURL = StringHelper.GetPath(fi[j].FullName);
                        strHtml += strLine.Replace("[strSRC]", Url.Content(strURL));
                        strHtml = strHtml.Replace("[strFileName]", fi[j].Name);
                        strHtml = strHtml.Replace("[strVAL]", strURL.Replace("//", "/"));
                    }
                    strHtml += "</div>";
                }
            }
            else
            {
                for (int i = 1; i <= (totalRow - 1); i++)
                {
                    strHtml += "<div class='img-line'>";
                    for (int j = ((i - 1) * numCol); j < (i * numCol); j++)
                    {
                        string strURL = StringHelper.GetPath(fi[j].FullName);
                        strHtml += strLine.Replace("[strSRC]", Url.Content(strURL));
                        strHtml = strHtml.Replace("[strFileName]", fi[j].Name);
                        strHtml = strHtml.Replace("[strVAL]", strURL.Replace("//", "/"));
                    }
                    strHtml += "</div>";
                }
                strHtml += "<div class='img-line'>";
                for (int i = 0; i < numOut; i++)
                {
                    string strURL = StringHelper.GetPath(fi[((totalRow - 1) * numCol) + i].FullName);
                    strHtml += strLine.Replace("[strSRC]", Url.Content(strURL));
                    strHtml = strHtml.Replace("[strFileName]", fi[((totalRow - 1) * numCol) + i].Name);
                    strHtml = strHtml.Replace("[strVAL]", strURL.Replace("//", "/"));

                }
                strHtml += "</div>";
            }
            strHtml += "</div>";
            ViewData["strHTML"] = strHtml;
            return View();
        }

    }
}
